import {createRouter, createWebHistory} from 'vue-router'
import {
    ACTIVITY_MANAGER, COMPLEX_MANAGER,
    EVENT_MANAGER, FeedBack_Handle,
    NEW_MANAGER,
    Notice_MANAGER, PHOTO_MANAGER,
    ROLE_NO_TEACHER,
    SUPER_ADMIN, UNIT_HUMAN
} from "@/util/config.js";

const route= [
    {
        path: '/login',
        name: 'pcLogin',
        component: () => import('../views/login/Login.vue')
    },
    {
        path: '/photo',
        name: '照片墙',
        component: () => import('../views/photo/Photo.vue')
    },
    {
        path: '/backPhoto',
        name: '照片墙背景',
        component: () => import('../views/photo/BackPhoto.vue')
    },
    {
        path: '/home',
        name: 'pc首页',
        component: ()=>import("@/views/home/Home.vue"),
    },
    {
        path: '/',
        component: ()=>import("@/layout/pc_Layout.vue"),
        redirect: "/home",
        children: [
            {
                path: 'message',
                name: '信息',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER,SUPER_ADMIN]
                },
                component: () => import('../views/student/StudentMessage.vue')
            },
            {
                path: 'updateAward',
                name: '上传奖惩罚、竞赛信息',
                component: () => import('../views/award/UpdateAward.vue')
            },
            {
                path: 'awardMessage',
                name: '奖惩信息',
                component: () => import('../views/award/AwardMessage.vue')
            },
            {
                path: 'allAwardMessage',
                name: '全部奖惩信息',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER,SUPER_ADMIN]
                },
                component: () => import('../views/award/allAwardMessage.vue')
            },
            {
                path: 'updateInternship',
                name: '上传实习经历',
                component: () => import('../views/student/UpdateInternship.vue')
            },
            {
                path: 'updateProject',
                name: '上传项目经历',
                component: () => import('../views/student/UpdateProject.vue')
            },
            {
                path: 'instructor',
                name: '辅导员',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER,SUPER_ADMIN]
                },
                component: () => import('../views/teacher/Instructor.vue')
            },
            {
                path: 'unit',
                name: '部门信息',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER,SUPER_ADMIN]
                },
                component: () => import('../views/unit/Unit.vue')
            },
            {
                path: 'unitHuman',
                name: '部门人员管理',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, UNIT_HUMAN]
                },
                component: () => import('../views/unit/UnitHuman.vue')
            },
            {
                path: 'resetPwd',
                name: '修改密码',
                component: () => import('../views/login/RasePwd.vue')
            },
            {
                path: 'uploadMessage',
                name: '更新学生信息',
                component: () => import('../views/student/UploadMessage.vue')
            },
            {
                path: 'Leave',
                name: '请假条',
                component: () => import('../views/student/Leave.vue')
            },
            {
                path: 'photoManage',
                name: '图片管理',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, PHOTO_MANAGER]
                },
                component: () => import('../views/photo/PhotoManage.vue')
            },
            {
                path: 'log',
                name: '日志',
                component: () => import('../views/About/Log.vue')
            },
            {
                path: 'activity',
                name: '活动查询',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, ACTIVITY_MANAGER]
                },
                component: () => import('../views/activities/Activity.vue')
            },
            {
                path: 'myActivity',
                name: '我的活动',
                component: () => import('../views/activities/Activity.vue')
            },
            {
                path: 'notifications',
                name: '通知详情',
                component: () => import('../views/notice/Notifications.vue')
            },
            {
                path: 'noticesEdit',
                name: '通知编辑页面',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, Notice_MANAGER]
                },
                component: () => import('../views/notice/NoticesEditor.vue')
            },
            {
                path: 'notice_manage',
                name: '通知管理',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, Notice_MANAGER]
                },
                component: () => import('../views/notice/NoticeManage.vue')
            },
            {
                path: 'news',
                name: '新闻详情',
                component: () => import('../views/news/News.vue')
            },
            {
                path: 'newsHistory',
                name: '历史新闻',
                component: () => import('../views/news/NewsHistory.vue')
            },
            {
                path: 'newsEdit',
                name: '新闻编辑页面',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, NEW_MANAGER]
                },
                component: () => import('../views/news/NewsEditor.vue')
            },
            {
                path: 'activities',
                name: '活动详情',
                component: () => import('../views/activities/Activities.vue')
            },
            {
                path: 'complex',
                name: '综测表',
                component: () => import('../template/Complex.vue')
            },
            {
                path: 'UploadComplex',
                name: '上传综测',
                component: () => import('../views/complex/UploadComplex.vue')
            },
            {
                path: 'MyFeedBack',
                name: '我的反馈',
                component: () => import('../views/feedBack/MyFeedBack.vue')
            },
            {
                path: 'ComplexApproval',
                name: '综测审批',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, COMPLEX_MANAGER]
                },
                component: () => import('../views/complex/ComplexApproval.vue')
            },
            {
                path: 'getAllComplex',
                name: '综测查询',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, COMPLEX_MANAGER]
                },
                component: () => import('../views/complex/GetAllComplex.vue')
            },
            {
                path: 'ProjectMessage',
                name: '项目查询',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN]
                },
                component: () => import('../views/student/ProjectMessage.vue')
            },
            {
                path: 'FieldworkMessage',
                name: '实习查询',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN]
                },
                component: () => import('../views/student/FieldworkMessage.vue')
            },
            {
                path: 'Contributors',
                name: '贡献者',
                component: () => import('../views/About/Contributors.vue')
            },
            {
                path: 'activityHistory',
                name: '历史活动',
                component: () => import('../views/activities/ActivityHistory.vue')
            },
            {
                path: 'unitHuman',
                name: '部门人员管理',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, UNIT_HUMAN]
                },
                component: () => import('../views/unit/UnitHuman.vue')
            },
            {
                path: 'myUnit',
                name: '我的部门',
                component: () => import('../views/unit/MyUnit.vue')
            },
            {
                path: 'importantEvents',
                name: '大事提醒',
                component: () => import('../views/event/ImportantEvents.vue')
            },
            {
                path: 'eventManage',
                name: '大事管理',
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, EVENT_MANAGER]
                },
                component: () => import('../views/event/EventManage.vue')
            },
            {
                path: "/403",
                name: "403",
                component: () => import('../component/ErrorMessage/403.vue')
            },
            {
                path: "/404",
                name: "404",
                component: () => import('../component/ErrorMessage/404.vue')
            },
            {
                path: "/500",
                name: "500",
                component: () => import('../component/ErrorMessage/500.vue')
            },
            {
                path: "/502",
                name: "502",
                component: () => import('../component/ErrorMessage/502.vue')
            },
            {
                path: 'edit',
                name: "edit",
                component: () => import('../component/editor.vue')
            },
            {
                path: 'construction',
                name: "Construction",
                component: () => import('../views/student/Construction.vue')
            },
            {
                path: 'feedBackHandle',
                name: "意见反馈处理",
                meta: {
                    requireAuth: true,
                    roleNo: [ROLE_NO_TEACHER, SUPER_ADMIN, FeedBack_Handle]
                },
                component: () => import('../views/feedBack/FeedbackHandle.vue')
            }
        ]
    },

]


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: route
})

router.beforeEach((to, from, next) => {


    if ((to.path ==="/backPhoto")){
        next()
    }


    //判断用户是否登录
    if (!localStorage.getItem('user') && (to.path !== "/login")) {
        // 用户未登录，跳转到登录界面
        next("/login");
    }

    if (to.meta.requireAuth) {
        //权限判断
        let flag = false
        let roleNo = []
        if (JSON.parse(localStorage.getItem("user")).roleNo.includes(",")) {
            roleNo = JSON.parse(localStorage.getItem("user"))["roleNo"].split(',')
        } else {
            roleNo.push(JSON.parse(localStorage.getItem("user"))["roleNo"])
        }
        roleNo.forEach(value => {
            if (to.meta.roleNo.includes(value)) {
                flag = true
            }
        })
        if (!flag) {
            next("/403")
        }
    }
    next()
})

export default router


